package com.base.library;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;

import com.base.library.databinding.ActivityTopBarBaseBinding;


/**
 * Created by xiechengzhi on 2017/5/3.
 */

public abstract class TopBarBaseActivity extends BaseActivity {
    ActivityTopBarBaseBinding activityTopBarBaseBinding;
    private ViewDataBinding contentViewBinding;
    private View.OnClickListener onClickListenerTopLeft;
    private View.OnClickListener onClickListenerTopRight;
    private int menuResId;
    private String menuStr;

    @Override
    public int getLayoutId() {
        return R.layout.activity_top_bar_base;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activityTopBarBaseBinding = getViewDataBinding();
        contentViewBinding = DataBindingUtil.inflate(LayoutInflater.from(TopBarBaseActivity.this), getContentView(), activityTopBarBaseBinding.viewContent, true);
        initContentView(savedInstanceState);
    }

    public <T extends ViewDataBinding> T getContentViewBinding() {
        return (T) contentViewBinding;
    }

    public void setTitle(String title) {
        if (!com.base.library.utils.StringUtils.isEmpty(title)) {
            activityTopBarBaseBinding.tvTitle.setText(title);
        }
    }

    public void setTitle(@StringRes int title) {
        activityTopBarBaseBinding.tvTitle.setText(title);
    }

    protected void setTopLeftButton(View.OnClickListener onClickListener) {
        this.onClickListenerTopLeft = onClickListener;
        activityTopBarBaseBinding.btnBack.setOnClickListener(onClickListener);
    }

    protected void setTopLeftButton(int iconResId, View.OnClickListener onClickListener) {
        activityTopBarBaseBinding.btnBack.setImageResource(iconResId);
        this.onClickListenerTopLeft = onClickListener;
        activityTopBarBaseBinding.btnBack.setOnClickListener(onClickListener);
    }

    protected void setTopRightButton(String menuStr, View.OnClickListener onClickListener) {
        this.onClickListenerTopRight = onClickListener;
        this.menuStr = menuStr;
        activityTopBarBaseBinding.btnRightText.setOnClickListener(onClickListener);
        activityTopBarBaseBinding.btnRightText.setText(menuStr);
        activityTopBarBaseBinding.btnRightText.setVisibility(View.VISIBLE);
        activityTopBarBaseBinding.btnRightImg.setVisibility(View.GONE);
    }

    protected void setTopRightButton(int menuResId, View.OnClickListener onClickListener) {
        this.menuResId = menuResId;
        this.onClickListenerTopRight = onClickListener;
        activityTopBarBaseBinding.btnRightImg.setOnClickListener(onClickListener);
        activityTopBarBaseBinding.btnRightImg.setImageResource(menuResId);
        activityTopBarBaseBinding.btnRightImg.setVisibility(View.VISIBLE);
        activityTopBarBaseBinding.btnRightText.setVisibility(View.GONE);
    }

    public abstract int getContentView();

    public abstract void initContentView(Bundle savedInstanceState);
}
